﻿<!--
* 开源版本请务必保留此注释头信息，若删除捷码开源〔GEMOS〕官方保留所有法律责任追究！
* 本软件受国家版权局知识产权以及国家计算机软件著作权保护（登记号：2018SR503328）
* 不得恶意分享产品源代码、二次转售等，违者必究。
* Copyright (c) 2020 gemframework all rights reserved.
* http://www.gemframework.com
* 版权所有，侵权必究！
-->
<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="common/links :: common_header(~{::title},~{},~{})">
    <title>用户管理</title>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户名:</label>
                        <div class="layui-input-inline">
                            <input name="username" class="layui-input" placeholder="输入账号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">真实姓名:</label>
                        <div class="layui-input-inline">
                            <input name="realname" class="layui-input" placeholder="输入用户名"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">性&emsp;别:</label>
                        <div class="layui-input-inline">
                            <select name="sex">
                                <option value="">选择性别</option>
                                <option value="0">男</option>
                                <option value="1">女</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a shiro:hasPermission="user:update" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a shiro:hasPermission="user:delete" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a shiro:hasPermission="user:resetPass" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reset">重置密码</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="userStatus">
    <input type="checkbox" lay-filter="editStateBtn" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.status==0?'checked':''}}  {{d.id==1?'disabled':''}} style="display: none;"/>
    <p style="display: none;">{{d.status==0?'正常':'锁定'}}</p>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">直属机构:</label>
            <div class="layui-input-block">
                <div id="selectTree" class="ew-xmselect-tree"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色授权:</label>
            <div class="layui-input-block">
                <div id="userEditRoleSel"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">用&nbsp;&nbsp;户&nbsp;&nbsp;名:</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入账号" class="layui-input"
                       lay-verType="tips" maxlength="15" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">设置密码:</label>
            <div class="layui-input-block">
                <input name="password" placeholder="默认123456" class="layui-input" type="password"
                       lay-verType="tips" maxlength="32" value="123456"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">真实姓名:</label>
            <div class="layui-input-block">
                <input name="realname" placeholder="请输入用户名" class="layui-input"
                       lay-verType="tips" maxlength="10" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">选择性别:</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="0" title="男" checked/>
                <input type="radio" name="sex" value="1" title="女"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">手机号码:</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="请输入手机号" class="layui-input"
                       lay-verType="tips" lay-verify="phone" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">电子邮箱:</label>
            <div class="layui-input-block">
                <input name="email" placeholder="请输入电子邮箱" class="layui-input"
                       lay-verType="tips" lay-verify="email" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分1 -->
<div th:replace="common/scripts :: common"></div>
<div th:replace="common/scripts :: custom"></div>
<div th:replace="common/shiro :: userShiroTag"></div>
<script th:inline="javascript">
    layui.use(['layer', 'form', 'table','tableX', 'util', 'admin', 'xmSelect', 'treeTable'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        var treeTable = layui.treeTable;

        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {type: 'numbers'},
            {field: 'username', title: '账号', sort: true},
            {field: 'realname', title: '用户名', sort: true},
            {field: 'phone', title: '手机号', sort: true},
            {
                field: 'roles', title: '角色', templet: function (d) {
                    return d.roles.map(function (item) {
                        return '<span class="layui-badge layui-badge-gray">' + item.name + '</span>';
                    }).join('&nbsp;&nbsp;');
                }, sort: true, width: 150
            },
            {
                field: 'createTime', title: '创建时间', templet: function (d) {
                    return util.toDateString(d.createTime);
                }, sort: true
            },

            {field: 'deleted', title: '状态', templet: '#userStatus', sort: true, width: 100},
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 200}
        ]

        var insTb = table.render({
            elem: '#dataTable',
            url: 'user/page',
            page: true,
            toolbar: [shiroToolbar(shiroSave,shiroDelete)].join(''),
            cellMinWidth: 100,
            cols: [cols],
            done: function () {
                // 绑定鼠标右键
                tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(shiroDelete,shiroUpdate));
            }
        });

        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj.data);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        window.showEditModel = function(mData) {
            //管理员不让删除
            if(mData && mData.id === 1){
                return layer.msg('包含系统预设数据，禁止删除', {icon: 4});
            }

            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加') + '用户',
                content: $('#formDialog').html(),
                shadeClose:true,
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('dataForm', mData);
                    // 表单提交事件
                    form.on('submit(saveBtn)', function (data) {
                        data.field.roleIds = insRoleSel.getValue('valueStr');
                        data.field.deptId = insDeptXmSel.getValue('valueStr');
                        var loadIndex = layer.load(2);
                        if(data.field.password === ""){
                            delete data.field.password;
                        }
                        $.post(mData?'user/update':'user/save', data.field, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });

                    var insDeptXmSel;
                    $.get('dept/list', function (res) {
                        if (res.code === 0) {
                            for (var i = 0; i < res.data.length; i++) {
                                res.data[i].title = res.data[i].name;
                                res.data[i].id = res.data[i].id;
                                res.data[i].spread = true;
                            }
                            treeData = treeTable.pidToChildren(res.data, 'id', 'pid');
                            // 渲染下拉树
                            insDeptXmSel = layui.xmSelect.render({
                                el: '#selectTree',
                                height: '250px',
                                data: treeData,
                                filterable:'true',//开启搜索
                                initValue: [mData ? mData.deptId: 1],
                                model: {label: {type: 'text'}},
                                prop: {
                                    name: 'name',
                                    value: 'id'
                                },
                                radio: true,
                                clickClose: true,
                                tree: {
                                    show: true,
                                    indent: 15,
                                    strict: false,
                                    expandedKeys: true
                                }
                            });
                        }else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },'json');


                    //查询角色list
                    var insRoleSel;
                    $.get('role/list', {}, function (res) {
                        if (res.code === 0) {
                            // 渲染多选下拉框
                            insRoleSel = xmSelect.render({
                                el: '#userEditRoleSel',
                                name: 'userEditRoleSel',
                                tips:'请授权角色',
                                layVerify: 'required',
                                layVerType: 'tips',
                                direction: 'auto',
                                filterable: true,
                                paging: true,
                                pageSize: 5,
                                model: {
                                    label: {
                                        type: 'block',
                                        block: {
                                            //最大显示数量, 0:不限制
                                            showCount: 5,
                                            //是否显示删除图标
                                            showIcon: true,
                                        }
                                    }
                                },
                                toolbar: {
                                    show: true,
                                },
                                prop: {
                                    name: 'name',
                                    value: 'id',
                                },
                                data: res.data,
                            });
                            insRoleSel.set
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                        // 回显选中角色
                        if (mData && mData.roles) {
                            // insRoleSel.setValue([{name:"a",id:"1"},{name:"a2",id:"2"}].map(function (item) {
                            insRoleSel.setValue(mData.roles.map(function (item) {
                                return item.id;
                            }));
                        }
                    }, 'json');


                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        window.doDel = function(obj){
            //管理员不让删除
            if(obj.id === 1 || (obj.ids && obj.ids.indexOf(1) > -1)){
                return layer.msg('包含系统预设数据，禁止删除', {icon: 4});
            }
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('user/delete', {
                    id: obj ? obj.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 修改用户状态 */
        form.on('switch(editStateBtn)', function (obj) {
            //管理员不让禁用
            if(obj.elem.value === "1"){
                layer.msg('管理员帐号状态，禁止修改', {icon: 4});
                return;
            }
            var loadIndex = layer.load(2);
            $.post('user/status', {
                id: obj.elem.value,
                status: obj.elem.checked ? 0 : 1
            }, function (res) {
                layer.close(loadIndex);
                if (res.code === 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        /* 重置密码 */
        function resetPsw(obj) {
            layer.confirm('确定要重置“' + obj.data.realname + '”的登录密码为123456吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('user/resetPass', {
                    id: obj.data.id,
                    password: '123456'
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
    });
</script>
</body>
</html>